package com.jmu.edu.IMApplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.jmu.edu.IMApplication.R;

import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

public class SwipeMessageItemAdapter extends BaseSwipeAdapter {

    private Context context;
    // 用来判断是否有滑动菜单打开,只要有一个打开就是true
    private boolean isOpen;


    private List<Conversation> conversationList;


    public boolean isOpen() {
        return isOpen;
    }

    public SwipeMessageItemAdapter(Context context, List<Conversation> conversationList) {
        this.context = context;
        this.conversationList = conversationList;
        for (Conversation conversation : conversationList) {
            Log.d("conversion", conversation.toJson());

        }
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.fragment_message_swipe;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View view = View.inflate(context, R.layout.fragment_message_list_item, null);

        SwipeLayout swipeLayout = view.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.setSwipeEnabled(true);
        // 设置SwipeLayout 监听
        swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                isOpen = true;
            }

            @Override
            public void onOpen(SwipeLayout layout) {

            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {
                isOpen = false;
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });


        return view;
    }

    @Override
    public void fillValues(int position, View convertView) {
        ImageView avatarImage = convertView.findViewById(R.id.fragment_message_friend_avatar);
        TextView nicknameText = convertView.findViewById(R.id.fragment_message_friend_nickname);
        TextView lastChatRecordText = convertView.findViewById(R.id.fragment_message_friend_last_chat_record);
        TextView lastChatRecordTimeText = convertView.findViewById(R.id.fragment_message_friend_chat_last_record_time);
        ImageButton topButton = convertView.findViewById(R.id.fragment_message_swipe_top_button);
        ImageButton deleteButton = convertView.findViewById(R.id.fragment_message_swipe_delete_button);


        Conversation conversation = conversationList.get(position);


        //avatarImage.setImageResource(R.drawable.smc);
        UserInfo userInfo = (UserInfo) conversation.getTargetInfo();
        userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (bitmap == null) {
                    avatarImage.setImageResource(R.drawable.smc);
                } else {
                    avatarImage.setImageBitmap(bitmap);
                }
            }
        });

        nicknameText.setText(userInfo.getUserName());

        lastChatRecordText.setText(conversation.getLatestText());
//        Timestamp timestamp = new Timestamp(conversation.getLatestMessage().getCreateTime());
//        String dateString = timestamp.toString().substring(0, timestamp.toString().indexOf("."));
//        lastChatRecordTimeText.setText(dateString);

    }

    @Override
    public int getCount() {
        return conversationList.size();
    }

    @Override
    public Conversation getItem(int position) {
        return conversationList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
